package icu.d4peng.basic.http.forest;

import icu.d4peng.basic.http.forest.annotation.ForestScannerRegister;
import icu.d4peng.basic.http.forest.properties.ForestConfigurationProperties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Import;

import com.dtflys.forest.config.SpringForestProperties;
import com.dtflys.forest.interceptor.SpringInterceptorFactory;
import com.dtflys.forest.reflection.SpringForestObjectFactory;
import com.dtflys.forest.spring.ForestBeanProcessor;

/**
 * @author d4peng d4peng@qq.com
 * @date 2023-11-26 15:36:16
 */

@Configuration
@EnableConfigurationProperties({ ForestConfigurationProperties.class })
@Import({ ForestScannerRegister.class })
@ConditionalOnProperty(prefix = ForestConfigurationProperties.PREFIX, name = "enabled", havingValue = "true",
		matchIfMissing = true)
public class ForestAutoConfiguration {

	@Bean
	@ConditionalOnMissingBean
	public SpringForestProperties forestProperties() {
		return new SpringForestProperties();
	}

	@Bean
	@ConditionalOnMissingBean
	public SpringForestObjectFactory forestObjectFactory() {
		return new SpringForestObjectFactory();
	}

	@Bean
	@ConditionalOnMissingBean
	public SpringInterceptorFactory forestInterceptorFactory() {
		return new SpringInterceptorFactory();
	}

	@Bean
	@ConditionalOnMissingBean
	public ForestBeanProcessor forestBeanProcessor() {
		return new ForestBeanProcessor();
	}

	@Bean
	@DependsOn("forestBeanProcessor")
	@ConditionalOnMissingBean
	public ForestBeanRegister forestBeanRegister(ConfigurableApplicationContext applicationContext,
			SpringForestProperties properties, SpringForestObjectFactory forestObjectFactory,
			SpringInterceptorFactory forestInterceptorFactory,
			ForestConfigurationProperties forestConfigurationProperties) {
		ForestBeanRegister register = new ForestBeanRegister(applicationContext, forestConfigurationProperties,
				properties, forestObjectFactory, forestInterceptorFactory);
		register.registerForestConfiguration();
		register.registerScanner();
		return register;
	}

}
